package com.loong.example;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;

import androidx.annotation.Nullable;

import com.loong.android.KeyCode;
import com.loong.android.LonWin;
import com.loong.android.tools.Clipboard;

public class ClipboardTestActivity extends LonWin {
    private final static String TAG = ClipboardTestActivity.class.getSimpleName();
    private Context context = null;
    private ClipboardManager clipboardManager;
    private int clipboardWriteIndex = 0;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_clipboard_test);
        context = this;
        clipboardManager = Clipboard.init(context);
        findViewById(R.id.btn_clipboard_write).setOnClickListener(v -> {
            String writeContent = "clipboard-" + (clipboardWriteIndex++);
            ClipData clipData = ClipData.newPlainText("label:loong", writeContent);
            clipboardManager.setPrimaryClip(clipData);
        });
        findViewById(R.id.btn_clipboard_read).setOnClickListener(v -> {
            Log.e(TAG, "Clipboard: hasClipData: " + clipboardManager.hasPrimaryClip());
            ClipData clipData = clipboardManager.getPrimaryClip();
            Log.e(TAG, "Clipboard: hasClipData: " + (clipData != null ? clipData.getItemCount() : -1));
            if(clipData != null) {
                int itemSize = clipData.getItemCount();
                if(itemSize > 0) {
                    for (int i = 0; i < itemSize; i++) {
                        Log.e(TAG, "Clipboard: item: " + clipboardManager.getPrimaryClip().getItemAt(i).getText());
                    }
                }
            }
        });
        findViewById(R.id.btn_close).setOnClickListener(v -> {
            finish();
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyCode.BACK) {
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}